{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f50c7a0c-cb29-4457-8cf6-8a3a004bbf8c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T15:57:01.147186Z",
     "iopub.status.busy": "2022-05-21T15:57:01.146913Z",
     "iopub.status.idle": "2022-05-21T15:57:01.410566Z",
     "shell.execute_reply": "2022-05-21T15:57:01.409966Z",
     "shell.execute_reply.started": "2022-05-21T15:57:01.147119Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d088e63a-9b97-4628-960e-c4efe63aa3b9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T15:57:02.778960Z",
     "iopub.status.busy": "2022-05-21T15:57:02.778633Z",
     "iopub.status.idle": "2022-05-21T15:57:02.793500Z",
     "shell.execute_reply": "2022-05-21T15:57:02.792869Z",
     "shell.execute_reply.started": "2022-05-21T15:57:02.778935Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>16</td>\n",
       "      <td>17</td>\n",
       "      <td>18</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    0   1   2   3\n",
       "0   0   1   2   3\n",
       "1   4   5   6   7\n",
       "2   8   9  10  11\n",
       "3  12  13  14  15\n",
       "4  16  17  18  19"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据\n",
    "data = pd.DataFrame(np.arange(20).reshape(5, 4))\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e75c45bd-0bfa-45a5-8b2a-5ea0f2bbd1a5",
   "metadata": {},
   "source": [
    "## 一、重新排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ecbf4044-e513-48f1-aaf7-a3a479355e9e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T15:59:50.590498Z",
     "iopub.status.busy": "2022-05-21T15:59:50.590066Z",
     "iopub.status.idle": "2022-05-21T15:59:50.596234Z",
     "shell.execute_reply": "2022-05-21T15:59:50.595272Z",
     "shell.execute_reply.started": "2022-05-21T15:59:50.590471Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 0, 4, 1, 3])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成一组随机排列序数\n",
    "newOrder = np.random.permutation(5)\n",
    "newOrder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8f348acb-9cba-4eb6-bc77-ba5a3a22ec4c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T16:01:48.775565Z",
     "iopub.status.busy": "2022-05-21T16:01:48.775244Z",
     "iopub.status.idle": "2022-05-21T16:01:48.783671Z",
     "shell.execute_reply": "2022-05-21T16:01:48.782860Z",
     "shell.execute_reply.started": "2022-05-21T16:01:48.775540Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>16</td>\n",
       "      <td>17</td>\n",
       "      <td>18</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    0   1   2   3\n",
       "2   8   9  10  11\n",
       "0   0   1   2   3\n",
       "4  16  17  18  19\n",
       "1   4   5   6   7\n",
       "3  12  13  14  15"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按数组执行排列,reindex\n",
    "data.reindex(newOrder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "52eb76d9-942c-4697-a049-a1eb026cf829",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T16:01:51.437126Z",
     "iopub.status.busy": "2022-05-21T16:01:51.436698Z",
     "iopub.status.idle": "2022-05-21T16:01:51.444681Z",
     "shell.execute_reply": "2022-05-21T16:01:51.443926Z",
     "shell.execute_reply.started": "2022-05-21T16:01:51.437099Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>16</td>\n",
       "      <td>17</td>\n",
       "      <td>18</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    0   1   2   3\n",
       "2   8   9  10  11\n",
       "0   0   1   2   3\n",
       "4  16  17  18  19\n",
       "1   4   5   6   7\n",
       "3  12  13  14  15"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按数组执行排列,take\n",
    "data.take(newOrder)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39198317-9a92-4ef3-9d94-9d04f4f9dc67",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-20T07:31:02.184105Z",
     "iopub.status.busy": "2022-05-20T07:31:02.183676Z",
     "iopub.status.idle": "2022-05-20T07:31:02.195421Z",
     "shell.execute_reply": "2022-05-20T07:31:02.194653Z",
     "shell.execute_reply.started": "2022-05-20T07:31:02.184079Z"
    },
    "tags": []
   },
   "source": [
    "## 二、随机取样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "3312e542-3aff-4130-9fcf-7a132fe96b15",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T16:23:58.585939Z",
     "iopub.status.busy": "2022-05-21T16:23:58.585618Z",
     "iopub.status.idle": "2022-05-21T16:23:58.594440Z",
     "shell.execute_reply": "2022-05-21T16:23:58.593666Z",
     "shell.execute_reply.started": "2022-05-21T16:23:58.585915Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0  1   2   3\n",
       "2  8  9  10  11\n",
       "1  4  5   6   7\n",
       "0  0  1   2   3"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 随机取样，使用sample函数\n",
    "data.sample(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "1c145301-8ce9-4e0f-9a86-1b6b4c138d32",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T16:25:18.468904Z",
     "iopub.status.busy": "2022-05-21T16:25:18.468237Z",
     "iopub.status.idle": "2022-05-21T16:25:18.478757Z",
     "shell.execute_reply": "2022-05-21T16:25:18.477997Z",
     "shell.execute_reply.started": "2022-05-21T16:25:18.468874Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>16</td>\n",
       "      <td>17</td>\n",
       "      <td>18</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    0   1   2   3\n",
       "2   8   9  10  11\n",
       "1   4   5   6   7\n",
       "4  16  17  18  19\n",
       "0   0   1   2   3\n",
       "2   8   9  10  11\n",
       "3  12  13  14  15"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 随机取样，使用sample函数,允许重复采样\n",
    "data.sample(6, replace=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
